﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TransporteAgua
{
    public partial class frmPrincipal : Form
    {
        public string host, database, user, password;
        Database transporteAgua;
        frmLogin login;
        public frmOperadores operadores;
        public frmClientes clientes;
        public frmTomarPedidos tomarPedido;
        public frmPedidosPendientes pendientes;
        public frmPedidosPorCobrar porCobrar;
        public frmEnviarPedido enviarPedido;
		public frmCobranza cobranza;
		public frmBuscar buscar;

        public frmPrincipal()
        {
            InitializeComponent();
            host = "localhost";
            database = "transporte de agua";
            login = new frmLogin(this);
            login.Focus();
        }
        internal void conectar(string servidor, string name, string pass)
        {
            host = servidor;
            user = name;
            password = pass;
            try
            {
                transporteAgua = new Database(host, database, user, password);
                operadoresToolStripMenuItem.Enabled = true;
                clientesToolStripMenuItem.Enabled = true;
                pedidosToolStripMenuItem.Enabled = true;
                cerrarSesionToolStripMenuItem.Enabled = true;
                iniciarSesionToolStripMenuItem.Enabled = false;
                MessageBox.Show("Bienvenido " + user, "Login completado", MessageBoxButtons.OK, MessageBoxIcon.Information);
                operadores = new frmOperadores(transporteAgua, this);
                clientes = new frmClientes(transporteAgua, this);
                tomarPedido = new frmTomarPedidos(transporteAgua, this);
                pendientes = new frmPedidosPendientes(transporteAgua, this);
                porCobrar = new frmPedidosPorCobrar(transporteAgua, this);
                enviarPedido = new frmEnviarPedido(transporteAgua, this);
				buscar = new frmBuscar(transporteAgua, this);
				cobranza = new frmCobranza(transporteAgua, this);
                Focus();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.Message, "Error de base de datos", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmPrincipal_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (transporteAgua != null)
            {
                DialogResult res = MessageBox.Show(this, "Se cerrará la sesion y se saldrá del sistema. ¿Deseas continuar?", "Atencion!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (res == DialogResult.Cancel) e.Cancel = true;
                else
                {
                    cerrarSesionToolStripMenuItem_Click(this, new EventArgs());
                    e.Cancel = false;
                }
            }
            else {
                DialogResult res = MessageBox.Show(this, "¿Deseas salir del programa?", "Atencion!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (res == DialogResult.Cancel) e.Cancel = true;
                else e.Cancel = false;
            }
        }
        private void cerrarSesionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transporteAgua = null;
            operadoresToolStripMenuItem.Enabled = false;
            clientesToolStripMenuItem.Enabled = false;
            pedidosToolStripMenuItem.Enabled = false;
            cerrarSesionToolStripMenuItem.Enabled = false;
            iniciarSesionToolStripMenuItem.Enabled = true;

            MessageBox.Show("Hasta luego " + user, "Sesion cerrada.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void frmPrincipal_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (login != null) login.Dispose();
            if (operadores != null) operadores.Dispose();
            if (clientes != null) clientes.Dispose();
            if (tomarPedido != null) tomarPedido.Dispose();
        }

        private void mostrarTodosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            operadores.Show();
            operadores.Focus();
        }

        private void mostrarTodosToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            clientes.Show();
            clientes.Focus();
        }

        private void tomarPedidoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tomarPedido.agregar();
        }

        private void iniciarSesionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            login.Show(this);
            login.Focus();
        }

        private void pedidosPendientesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pendientes.Show();
            pendientes.Focus();
        }

        private void pedidosPorCobrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            porCobrar.Show();
            porCobrar.Focus();
        }

		private void menuBuscarOperador_Click(object sender, EventArgs e)
		{
			buscar.buscarPor("operadores");
		}

		private void menuBuscarClientes_Click(object sender, EventArgs e)
		{
			buscar.buscarPor("clientes");
		}
    }
}
